package com.ctakit.kiwi.server.web.config;

import com.ctakit.kiwi.web.view.MyMappingJacksonJsonView;
import com.ctakit.kiwi.web.view.resolver.JsonViewResolver;
import com.ctakit.kiwi.web.view.resolver.MultipleViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jack on 2017/7/16.
 */
@Configuration
@ComponentScan(basePackages = {"com.ctakit.web.controller", "com.ctakit.kiwi.server.web.controller"}, useDefaultFilters = false, includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})})
//继承WebMvcConfigurerAdapter类，重写其方法可对Spring MVC进行配置
// 开启Spring MVC支持，若无此句，重写WebMvcConfigurerAdapter方法无效
public class WebMvcConfig implements WebMvcConfigurer {


    /**
     * 配置文件上传Bean
     *
     * @return
     */
	@Bean
	public MultipartResolver multipartResolver() {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		multipartResolver.setMaxUploadSize(1000000);
		multipartResolver.setMaxInMemorySize(4096);
		return multipartResolver;
	}


    // Configuration for InternalResourceViewResolver (JSP)
    @Bean
    public MultipleViewResolver multipleViewResolver() {
        Map<String, ViewResolver> resolvers = new HashMap<>();
        resolvers.put("jsp", internalResourceViewResolver());
        resolvers.put("json", jsonViewResolver());
        resolvers.put("ftl", freeMarkerViewResolver());

        MultipleViewResolver resolver = new MultipleViewResolver();
        resolver.setResolvers(resolvers);
        return resolver;
    }


    // Configuration for InternalResourceViewResolver (JSP)
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() {
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPath("classpath:/templates/");
        // 其他配置...

        return configurer;
    }

    @Bean
    public FreeMarkerViewResolver freeMarkerViewResolver() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setPrefix("");
        resolver.setSuffix(".htm");
        resolver.setContentType("text/html;charset=UTF-8");
        // 设置优先级，数字越小优先级越高
        resolver.setOrder(1);
        // 其他配置...

        return resolver;
    }

    @Bean
    public JsonViewResolver jsonViewResolver() {
        JsonViewResolver resolver = new JsonViewResolver();
        resolver.setJsonView(new MyMappingJacksonJsonView());
        return resolver;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        // Register InternalResourceViewResolver for JSP
        registry.viewResolver(multipleViewResolver());

    }

    /**
     * c重写addInterceptors方法，注册拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // ThreadLocalUtilsInterceptor threadLocalUtilsInterceptor = new ThreadLocalUtilsInterceptor();
        // AccessInterceptor accessInterceptor = accessInterceptor();
        // accessInterceptor.setExcludeUrls(new String[] { "/user/login",  "/user/checkUserExist", "/user/loginOut",  });


        /////
        List<String> exclude = Arrays.asList("/v2/**", "/swagger-resources/**", "/api/**", "/webjars/**", "/res/**");
        // registry.addInterceptor(threadLocalUtilsInterceptor).excludePathPatterns(exclude);
        //	registry.addInterceptor(accessInterceptor).excludePathPatterns(exclude);;
        //	registry.addInterceptor(new PaginationInterceptor()).excludePathPatterns(exclude);

    }


    /**
     * 此方法是用来添加静态资源映射的
     * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。 需要重新指定静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        //registry.addResourceHandler("swagger-ui.html").addResourceLocations(
        //	"classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/META-INF/resources/");

    }


}